<template>
  <transition name="fade" appear>
    <div class="user-info-form">
      <div class="form-header">
        <h3 class="form-title">基本资料</h3>
        <p class="form-subtitle">更新您的基本个人信息</p>
      </div>
      
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="profile-form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" maxlength="30" placeholder="请输入用户昵称">
                <i slot="prefix" class="el-icon-user input-icon"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="form.sex" class="gender-radio">
                <el-radio label="0">
                  <i class="el-icon-male"></i> 男
                </el-radio>
                <el-radio label="1">
                  <i class="el-icon-female"></i> 女
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" maxlength="11" placeholder="请输入手机号码">
                <i slot="prefix" class="el-icon-mobile-phone input-icon"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" maxlength="50" placeholder="请输入邮箱地址">
                <i slot="prefix" class="el-icon-message input-icon"></i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        
        <div class="form-actions">
          <el-button type="primary" @click="submit" class="submit-btn">
            <i class="el-icon-check"></i> 保存修改
          </el-button>
          <el-button @click="close" class="cancel-btn">
            <i class="el-icon-close"></i> 取消
          </el-button>
        </div>
      </el-form>
    </div>
  </transition>
</template>

<script>
import { updateUserProfile } from "@/api/system/user"

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    }
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          this.form = { nickName: user.nickName, phonenumber: user.phonenumber, email: user.email, sex: user.sex }
        }
      },
      immediate: true
    }
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.user.phonenumber = this.form.phonenumber
            this.user.email = this.form.email
          })
        }
      })
    },
    close() {
      this.$tab.closePage()
    }
  }
}
</script>

<style lang="scss" scoped>
.user-info-form {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  
  .form-header {
    margin-bottom: 24px;
    border-bottom: 1px solid #ebeef5;
    padding-bottom: 16px;
    
    .form-title {
      font-size: 20px;
      font-weight: 600;
      color: #303133;
      margin: 0 0 8px 0;
    }
    
    .form-subtitle {
      font-size: 14px;
      color: #909399;
      margin: 0;
    }
  }
  
  .profile-form {
    .el-form-item {
      margin-bottom: 22px;
      
      ::v-deep .el-form-item__label {
        font-weight: 500;
        color: #606266;
      }
      
      ::v-deep .el-input__inner {
        border-radius: 6px;
        transition: all 0.3s ease;
        
        &:focus {
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
      }
      
      ::v-deep .el-input__prefix {
        left: 12px;
        
        .input-icon {
          color: #c0c4cc;
          font-size: 16px;
          transition: color 0.3s ease;
        }
      }
      
      ::v-deep .el-input__inner:focus + .el-input__prefix .input-icon {
        color: #409eff;
      }
      
      .gender-radio {
        ::v-deep .el-radio {
          margin-right: 20px;
          
          .el-radio__label {
            i {
              margin-right: 4px;
            }
          }
        }
      }
    }
    
    .form-actions {
      text-align: center;
      margin-top: 30px;
      
      .el-button {
        padding: 10px 24px;
        border-radius: 6px;
        font-weight: 500;
        transition: all 0.3s ease;
        
        &.submit-btn {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          border: none;
          margin-right: 16px;
          
          &:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
          }
          
          i {
            margin-right: 6px;
          }
        }
        
        &.cancel-btn {
          border: 1px solid #dcdfe6;
          color: #606266;
          
          &:hover {
            color: #409eff;
            border-color: #c6e2ff;
            background-color: #ecf5ff;
            transform: translateY(-2px);
          }
          
          i {
            margin-right: 6px;
          }
        }
      }
    }
  }
}

// 页面过渡动画
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}

// 响应式设计
@media (max-width: 768px) {
  .user-info-form {
    padding: 16px;
    
    .form-header {
      margin-bottom: 20px;
      padding-bottom: 12px;
      
      .form-title {
        font-size: 18px;
      }
      
      .form-subtitle {
        font-size: 13px;
      }
    }
    
    .profile-form {
      .el-form-item {
        margin-bottom: 18px;
        
        ::v-deep .el-form-item__label {
          font-size: 14px;
        }
        
        ::v-deep .el-input__inner {
          font-size: 14px;
        }
      }
      
      .form-actions {
        margin-top: 24px;
        
        .el-button {
          padding: 8px 20px;
          font-size: 14px;
          
          &.submit-btn {
            margin-right: 12px;
            margin-bottom: 10px;
          }
          
          &.cancel-btn {
            margin-bottom: 10px;
          }
        }
      }
    }
  }
}

@media (max-width: 480px) {
  .user-info-form {
    padding: 12px;
    
    .form-header {
      margin-bottom: 16px;
      padding-bottom: 10px;
      
      .form-title {
        font-size: 16px;
      }
      
      .form-subtitle {
        font-size: 12px;
      }
    }
    
    .profile-form {
      .el-form-item {
        margin-bottom: 16px;
        
        ::v-deep .el-form-item__label {
          font-size: 13px;
        }
        
        ::v-deep .el-input__inner {
          font-size: 13px;
          padding: 8px 10px;
        }
        
        ::v-deep .el-input__prefix {
          left: 10px;
          
          .input-icon {
            font-size: 14px;
          }
        }
      }
      
      .form-actions {
        margin-top: 20px;
        
        .el-button {
          padding: 6px 16px;
          font-size: 13px;
          width: 100%;
          margin-bottom: 10px;
          
          &.submit-btn {
            margin-right: 0;
          }
        }
      }
    }
  }
}
</style>
